/*************************************************************************
	> File Name: main.cpp
	> Author: 
	> Mail: 
	> Created Time: 2017年05月26日 星期五 21时03分48秒
 ************************************************************************/

#include <pthread.h>
#include <stddef.h>
#include <tbb/concurrent_hash_map.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <utility>

#include "../public/concurrentqueue.h"

using namespace std;

struct DataShuju__key
{
	void *mpTO = NULL;
	virtual bool operator==(const DataShuju__key &x) const
	{
		bool bResult = false;
		if(this->mpTO == x.mpTO)
		{
			bResult = true;
		}

		return bResult;
	}
};
struct __DataShuju__
{
	int now;
};

struct my_hash_compare {
    static size_t hash(const DataShuju__key& x ) {

    	static char *p = new char[127];
    	sprintf(p,"%p fuck",x.mpTO);

        size_t h = 0;
        for( const char* s = p; *s; ++s )
            h = (h*17)^*s;

        return h;
    }
    static bool equal(const DataShuju__key &x1,const DataShuju__key &x2)
    {
    	return x1 == x2;
    }
};
void test1();
typedef tbb::concurrent_hash_map<DataShuju__key,__DataShuju__,my_hash_compare>  juHashMap;
typedef tbb::concurrent_hash_map<DataShuju__key, __DataShuju__,my_hash_compare>::accessor juIte;
typedef tbb::concurrent_hash_map<DataShuju__key, __DataShuju__,my_hash_compare>::const_accessor juIteREad;
typedef tbb::concurrent_hash_map<DataShuju__key, __DataShuju__,my_hash_compare>::const_iterator iteJ;

juHashMap hh;

moodycamel::ConcurrentQueue<DataShuju__key> duilie;



void* thread1( void *arg )
{

	int count = 0;
    for(int i = 0 ; i < 100000;i++)
    {
    	char *pStr = new char[20];
    	sprintf(pStr,"item %d",i);


    	DataShuju__key key;
    	key.mpTO = pStr;
    	juIte a;
    	if(hh.insert(a,key))
    	{
        	__DataShuju__ ds;
        	ds.now = 300;
        	 a->second = ds;

        	 a.release();

        	 if(duilie.enqueue(key))
        	 {

        		   juIteREad dsj;
        		   hh.find(dsj,key);

        		 count++;
        		 cout<<"增加："<<count<<" 次"<<endl;
        	 }

    	}


    }

    cout<<"完成 "<<count<<" 次"<<endl;
    return NULL;
}
void SHow()
{
	int count = 0;
   for(iteJ rd = hh.begin();rd != hh.end();rd++)
   {
	   DataShuju__key dj = rd->first;

	 count++;
	 cout<<"查 2sadsadsadsadaa "<<count<<" 次"<<endl;
   }
}
void* thread3( void *arg )
{
	while(1)
	{
		SHow();
	}
}
void* thread2( void *arg )
{

	int count = 0;
	while(1)
	{
		DataShuju__key dj ;

		if(duilie.try_dequeue(dj))
		{
			juIteREad ji;
			//cout<<"rm shuju yes33"<<endl;
	    	if(hh.find(ji,dj))
	    	{
	    	//	cout<<"1次数:"<<count<<endl;
	    		//cout<<"rm shuju yes2"<<endl;
	    		hh.erase(ji);
	    		ji.release();
	    		hh.find(ji,dj);
	    	//	cout<<"rm shuju yes1"<<endl;
	    		count ++;
	    //	cout<<"删除了的:"<<count<<endl;
	    	}
	    	else
	    	{
	    		//count ++;
	    		//cout<<"失败次数:"<<count<<endl;
	    	}
		}
	}

    SHow();
    return NULL;
}

int main()
{
    cout<<"hello world!"<<endl;

    pthread_t th;
    pthread_t th2,th3;
    pthread_t th5,th6;
    int ret;
    int arg = 10;
    int *thread_ret = NULL;
    ret = pthread_create( &th, NULL, thread1, NULL);
    ret = pthread_create( &th5, NULL, thread1, NULL);

//    ret = pthread_create( &th3, NULL, thread3, NULL);
    ret = pthread_create( &th2, NULL, thread2, NULL);


  //  ret = pthread_create( &th6, NULL, thread3, NULL);


    sleep(300);


    return 0;
}

void test1()
{
    char *p = new char[127];
    char *p1 = new char[127];
    char * &pa= p;
    char * &pb = p;

    cout<<"qaa"<<endl;

    cout<<p<<endl;
    strcat(pa," yes!");
    cout<<p<<endl;
    pa = NULL;


    if(pa == pb)
    {
    	cout<<"ates!"<<endl;
    }

}
